<?php

use MysqliDb;
//use phpDocumentor\Reflection\Types\Integer;

class HuishengMysql
{
    private $db;
    public function __construct($config)
    {
        $this->db = new MysqliDb(
            $config['db_host'],
            $config['db_user'],
            $config['db_pass'],
            $config['db_database'],
            $config['db_port']
        );
    }
    public function getLiveSpeechDataByMid(String $mid)
    {
        $this->db->where('meeting_tag', $mid);
        $result = $this->db->getOne('rszh_meeting_live_speech');
        return $result;
    }

    public function insertDetailToSpeech(array $param)
    {
        $id = $this->db->insert('rszh_meeting_speech', $param);
        if (!$id) {
            throw new \Exception("insert failed");
        }
        return $id;
    }

    public function insertGrouptoVadSpeech(array $vads)
    {
        try {
            $this->db->startTransaction();
            $ids = $this->db->insertMulti('rszh_speech_vad_file', $vads);
            if (!$ids) {
                $this->db->rollback();
                throw new \Exception("insert failed");
            }
            $this->db->commit();
            return $ids;
        } catch (\Exception $e) {
            $this->db->rollback();
            throw new \Exception($e->getMessage());
        }
    }

    public function updateDetailToLiveSpeech(array $param, Int $id)
    {
        $this->db->where('id', $id);
        if (!$this->db->update('rszh_meeting_live_speech', $param)) {
            throw new \Exception("update failed");
        }
    }

    public function updateMistaketoLiveSpeech($id)
    {
        $this->db->where('id', $id)->update('rszh_meeting_live_speech', ["transition_state" => "3"]);
    }
}
